import collections
class Solution(object):
    def validTicTacToe(self, board):
        """
        :type board: List[str]
        :rtype: bool
        """
        x_count = 0
        o_count = 0
        x_win = 0
        o_win = 0
        for i in range(3):
            for j in range(3):
                if board[i][j] == 'X':
                    x_count += 1
                if board[i][j] == 'O':
                    o_count += 1
        if x_count == o_count or x_count == o_count + 1:
            for i in range(3):
                c = collections.Counter(board[i])
                if c['X'] == 3:
                    x_win += 1
                if c['O'] == 3:
                    o_win += 1
            for j in range(3):
                c = collections.Counter([board[i][j] for i in range(3)])
                if c['X'] == 3:
                    x_win += 1
                if c['O'] == 3:
                    o_win += 1
            c = collections.Counter([board[i][i] for i in range(3)])
            if c['X'] == 3:
                x_win += 1
            if c['O'] == 3:
                o_win += 1
            c = collections.Counter([board[i][2 - i] for i in range(3)])
            if c['X'] == 3:
                x_win += 1
            if c['O'] == 3:
                o_win += 1
            if x_win == 0:
                if o_win == 0:
                    return True
                else:
                    if x_count == o_count:
                        return True
                    else:
                        return False
            else:
                if o_win == 0:
                    if x_count == o_count + 1:
                        return True
                    else:
                        return False
                else:
                    return False
        else:
            return False


data = Solution()
board = ["XOX", " X ", "   "]
print(data.validTicTacToe(board))
